/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.controllers.effect;

import java.util.Collections;
import java.util.List;

import com.aionemu.gameserver.dataholders.DataManager;
import com.aionemu.gameserver.model.gameobjects.Creature;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.team.group.GroupEvent;
import com.aionemu.gameserver.network.aion.serverpackets.SM_ABNORMAL_EFFECT;
import com.aionemu.gameserver.network.aion.serverpackets.SM_ABNORMAL_STATE;
import com.aionemu.gameserver.skillengine.model.Effect;
import com.aionemu.gameserver.skillengine.model.SkillTargetSlot;
import com.aionemu.gameserver.skillengine.model.SkillTemplate;
import com.aionemu.gameserver.taskmanager.tasks.PacketBroadcaster.BroadcastMode;
import com.aionemu.gameserver.utils.PacketSendUtility;

/**
 * @author ATracer
 *
 */
public class PlayerEffectController extends EffectController
{

	/**
	 * current food effect
	 */
	private Effect	foodEffect;

	public PlayerEffectController(Creature owner)
	{
		super(owner);
	}

	@Override
	public void addEffect(Effect effect)
	{
		if (effect.isFood())
			addFoodEffect(effect);

		if (checkDuelCondition(effect))
			return;

		super.addEffect(effect);
		updatePlayerIconsAndGroup(effect);
	}

	@Override
	public void clearEffect(Effect effect)
	{
		if (effect.isFood())
			foodEffect = null;

		super.clearEffect(effect);
		updatePlayerIconsAndGroup(effect);
	}

	@Override
	public Player getOwner()
	{
		return (Player) super.getOwner();
	}

	/**
	 * @param effect
	 */
	private void updatePlayerIconsAndGroup(Effect effect)
	{
		if (!effect.isPassive())
		{
			updatePlayerEffectIcons();
			if (getOwner().isInGroup())
				getOwner().getPlayerGroup().updateGroupUIToEvent(getOwner().getObjectTemplate(), GroupEvent.UPDATE);
		}
	}

	/**
	 * Effect of DEBUFF should not be added if duel ended (friendly unit)
	 * @param effect
	 * @return
	 */
	private boolean checkDuelCondition(Effect effect)
	{
		Creature creature = effect.getEffector();
		if (creature instanceof Player)
		{
			if (getOwner().isFriend((Player) creature) && effect.getTargetSlot() == SkillTargetSlot.DEBUFF.ordinal())
				return true;
		}

		return false;
	}

	/**
	 * @param effect
	 */
	private void addFoodEffect(Effect effect)
	{
		if (foodEffect != null)
			foodEffect.endEffect();
		foodEffect = effect;
	}

	/**
	 * @param skillId
	 * @param skillLvl
	 * @param currentTime
	 * @param reuseDelay
	 */
	public void addSavedEffect(int skillId, int skillLvl, int currentTime)
	{
		SkillTemplate template = DataManager.SKILL_DATA.getSkillTemplate(skillId);
		int duration = template.getEffectsDuration();
		int remainingTime = duration - currentTime;

		if (remainingTime <= 0)
			return;

		Effect effect = new Effect(getOwner(), getOwner(), template, skillLvl, remainingTime);
		if (effect.isFood())
			addFoodEffect(effect);
		abnormalEffectMap.put(effect.getStack(), effect);
		effect.addAllEffectToSucess();
		effect.startEffect(true);

		PacketSendUtility.sendPacket(getOwner(), new SM_ABNORMAL_STATE(Collections.singletonList(effect), abnormals));

	}


	/**
	 *  Used when player see new player
	 *  
	 * @param player
	 */
	public void sendEffectIconsTo(Player player)
	{
		List<Effect> effects = getAbnormalEffects();
		PacketSendUtility.sendPacket(player, new SM_ABNORMAL_EFFECT(getOwner().getObjectId(), abnormals, effects));
	}
	

	public void updatePlayerEffectIcons()
	{
		getOwner().addPacketBroadcastMask(BroadcastMode.UPDATE_PLAYER_EFFECT_ICONS);
	}

	public void updatePlayerEffectIconsImpl()
	{
		List<Effect> effects = getAbnormalEffects();

		PacketSendUtility.sendPacket((Player) getOwner(), new SM_ABNORMAL_STATE(effects, abnormals));
	}
}
